package com.wms.service.impl;

import com.wms.bean.PageBean;
import com.wms.mapper.ZhiJianMapper;
import com.wms.service.ZhiJianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ZhiJianServiceImpl implements ZhiJianService {

    @Autowired
    ZhiJianMapper zhiJianMapper;

    @Override
    public PageBean<List<Map<String, Object>>> chaZhiJian(int page, int pageSize) {
        if (page<1){page=1;}
        if (pageSize<1||pageSize>100){pageSize=5;}
        // 计算偏移量（关键！分页必须用 offset = (page-1)*pageSize）
        int offset = (page - 1) * pageSize;
        long toaol=zhiJianMapper.zhijiantotal();
        List<Map<String,Object>> list=zhiJianMapper.chaZhiJian(offset,pageSize);
        PageBean pageBean=new PageBean<>();

        pageBean.setPage(offset);
        pageBean.setPageSize(pageSize);
        pageBean.setTotal(toaol);
        pageBean.setList(list);
        return pageBean;
    }

    //  评价
    @Override
    public int pingjia(String createBy,int inspectId,String content) {

        return zhiJianMapper.pingjia(createBy,inspectId,content);
    }

    @Override
    public int tupian(int id, String url) {
        return zhiJianMapper.tupian(id,url);
    }

    //采购质检 分页+查询
    @Override
    public PageBean<List<Map<String, Object>>> queryDataWithPage(String startTime, String endTime,String inspectUser,
                                                                 String supplierName,String purchaseId, String materialName,
                                                                 String materialCode, String batchNum,Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageNum < 1) {pageNum = 1;}
        if (pageSize == null || pageSize < 1) {pageSize = 5;}
        int offset = (pageNum - 1) * pageSize;
        List<Map<String, Object>> list=zhiJianMapper.queryData(startTime, endTime, inspectUser, supplierName, purchaseId,
                                                                materialName, materialCode, batchNum, offset, pageSize);
        Long total= zhiJianMapper.queryTotal(startTime, endTime, inspectUser, supplierName, purchaseId,
                                             materialName, materialCode, batchNum);
        // 计算总页数
        int totalPages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //  封装分页结果
        PageBean pageBean=new PageBean<>();
        pageBean.setPageSize(pageSize);//单页条数
//        pageBean.setPages(totalPages);//总页数
        pageBean.setTotal(total); //总数
        pageBean.setPage(offset);//页码
        pageBean.setList(list);

        return pageBean;
    }
}
